package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liangliang
 * @Date 2021/11/8 16:06 Administrator
 * @Description
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    /**
     * 分类物资的三级分类表数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
    /*    PageHelper.startPage(pageNum,pageSize);*/
        Example example = new Example(ProductCategory.class);

        List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(productCategoryList)){
            Map<Long, List<ProductCategory>> collect = productCategoryList.stream().collect(Collectors.groupingBy(productCategory -> {
                return productCategory.getPid();
            }));




            //获取0对应的所有顶级父类
            List<ProductCategoryTreeNodeVO> oneProductCategory = collect.get(0L).stream().map(productCategory -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
                productCategoryTreeNodeVO.setLev(1);
                Long id = productCategoryTreeNodeVO.getId();

                //根据一级ID，获得二级信息
                List<ProductCategory> productCategoryList2 = collect.get(id);
                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryList2.stream().map(productCategory2 -> {
                    ProductCategoryTreeNodeVO productCategoryTreeNodeVO2 = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(productCategory2,productCategoryTreeNodeVO2);
                    productCategoryTreeNodeVO2.setLev(2);
                    //根据二级ID得到三级信息
                    Long id2 = productCategoryTreeNodeVO2.getId();
                    List<ProductCategory> productCategoryList3 = collect.get(id2);//三级分类集合
                    if (!CollectionUtils.isEmpty(productCategoryList3)){
                        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVO3list = productCategoryList3.stream().map(productCategory3 -> {
                            ProductCategoryTreeNodeVO productCategoryTreeNodeVO3=new ProductCategoryTreeNodeVO();
                            BeanUtils.copyProperties(productCategory3,productCategoryTreeNodeVO3);
                            productCategoryTreeNodeVO3.setLev(3);
                            return productCategoryTreeNodeVO3;
                        }).collect(Collectors.toList());
                        productCategoryTreeNodeVO2.setChildren(productCategoryTreeNodeVO3list);

                    }
                    return productCategoryTreeNodeVO2;
                }).collect(Collectors.toList());
                productCategoryTreeNodeVO.setChildren(productCategoryTreeNodeVOList);
                return productCategoryTreeNodeVO;
            }).collect(Collectors.toList());
            //总条数
            int tatal = oneProductCategory.size();
            if (pageNum!=null && pageSize!=null){
                //没页数
                int size=(pageNum-1)*pageSize+pageSize;
                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = oneProductCategory.subList((pageNum - 1)*pageSize,size>tatal?tatal:size);
                PageVO<ProductCategoryTreeNodeVO> pageVO = new PageVO<>(tatal, productCategoryTreeNodeVOList);
                return  pageVO;
            }


        }
     return null;
    }

    /**
     * 获取父级的分类数据
     * @return
     */
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        Example example = new Example(ProductCategory.class);
        List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(example);
        //先根据ID分一个组
        Map<Long, List<ProductCategory>> listMap = productCategoryList.stream().collect(Collectors.groupingBy(productCategory -> {
            return productCategory.getPid();
        }));
        //获取所有的父级ID
        List<ProductCategory> collect = listMap.get(0L).stream().collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(collect)){
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = collect.stream().map(productCategory -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO1 = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO1);
                productCategoryTreeNodeVO1.setLev(1);
                //根据一级分类ID获取二级分类信息
                Long id2 = productCategoryTreeNodeVO1.getId();
                List<ProductCategory> twoProductCategory = listMap.get(id2).stream().collect(Collectors.toList());
                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS2 = twoProductCategory.stream().map(productCategory1 -> {
                    ProductCategoryTreeNodeVO productCategoryTreeNodeVO2 = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(productCategory1, productCategoryTreeNodeVO2);
                    productCategoryTreeNodeVO2.setLev(2);
                    return productCategoryTreeNodeVO2;
                }).collect(Collectors.toList());
                productCategoryTreeNodeVO1.setChildren(productCategoryTreeNodeVOS2);
                return productCategoryTreeNodeVO1;
            }).collect(Collectors.toList());
            return productCategoryTreeNodeVOList;
        }
        return null;

    }

    /**
     * 添加物资类别的分类
     * @param productCategory
     */
    @Override
    public void add(ProductCategory productCategory) {
        ProductCategory productCategory1 = new ProductCategory();
        /*Calendar calendar = Calendar.getInstance("yyyy-MM-dd HH:mm:ss");
        Date date = calendar.getTime();*/

        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = simpleDateFormat.format(date);
            date=simpleDateFormat.parse(now);
            productCategory1.setCreateTime(date);
            productCategory1.setModifiedTime(date);
            productCategory1.setName(productCategory.getName());
            productCategory1.setPid(productCategory.getPid());
            productCategory1.setRemark(productCategory.getRemark());
            productCategory1.setSort(productCategory.getSort());
            productCategoryMapper.insert(productCategory1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据ID查询物资类别的信息，用作数据回显
     * @param id
     * @return
     */
    @Override
    public ProductCategoryTreeNodeVO edit(Long id) {
        //如果传的ID是顶级父类ID，那么查询的就是父类信息，如果传的是父类下的二级信息那么查的就是二级的信息
        //如果修改的是顶级的父类的ID

        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        ProductCategory productCategory1 = productCategoryMapper.selectOne(productCategory);
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
        BeanUtils.copyProperties(productCategory1,productCategoryTreeNodeVO);
        return productCategoryTreeNodeVO;
    }

    /**
     * 修改物资类别信息
     * @param id
     * @param productCategoryVO
     */
    @Override
    public void update(Long id, ProductCategoryVO productCategoryVO) {

        try {
            ProductCategory productCategory1 = new ProductCategory();
            productCategory1.setId(id);
            ProductCategory productCategory2 = productCategoryMapper.selectOne(productCategory1);
            productCategory2.setSort(productCategoryVO.getSort());
            productCategory2.setRemark(productCategoryVO.getRemark());
            productCategory2.setPid(productCategoryVO.getPid());
            productCategory2.setName(productCategoryVO.getName());
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = simpleDateFormat.format(date);
            date=simpleDateFormat.parse(now);
            productCategory2.setModifiedTime(date);
            productCategoryMapper.updateByPrimaryKey(productCategory2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除物资类信息
     * @param id
     */
    @Override
    public void delete(Long id) {
        productCategoryMapper.deleteByPrimaryKey(id);
    }


}
